package com.tony.util.utils;

import java.time.*;
import java.util.Date;

/**
 * @author x
 * @date 2019/8/19 9:59
 */
public class DateConvertUtil {
    private static final ZoneId ZONE = ZoneId.systemDefault();

    /**
     * @param date java.util.Date
     * @return LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZONE);
    }

    /**
     * @param date java.util.Date
     * @return LocalDate
     */
    public static LocalDate dateToLocalDate(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZONE).toLocalDate();
    }

    /**
     * @param date java.util.Date
     * @return LocalTime
     */
    public static LocalTime dateToLocalTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZONE).toLocalTime();
    }

    /**
     * @param localDateTime java.time.LocalDateTime
     * @return java.util.Date
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZONE).toInstant());
    }

    /**
     * @param localDate java.time.LocalDate
     * @return java.util.Date
     */
    public static Date localDateToDate(LocalDate localDate) {
        Instant instant = localDate.atStartOfDay(ZONE).toInstant();
        return Date.from(instant);
    }

    /**
     * @param localTime java.time.LocalTime
     * @return java.util.Date
     */
    public static Date localTimeToDate(LocalTime localTime) {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), localTime);
        Instant instant = localDateTime.atZone(ZONE).toInstant();
        return Date.from(instant);
    }
}
